package NWish.properties.object3ds
{
	import NWish.Interface.ILogger;
	
	import NWish.json.OrderDict;
	import NWish.properties.Properties;
	
	import away3d.materials.utils.CubeMap;
	import away3d.primitives.SkyBox;
	
	public class SkyBoxProperties extends EntityProperties
	{
		public function SkyBoxProperties(parent)
		{
			super(parent);
			
			addPropertieKeys('SkyBox',[new SkyBoxMaterialProperties(parent)]);
			addPropertieKeys_read('SkyBox',['mouseDetails','numTriangles','castsShadows']);
		}
		
		override public function get type()
		{
			return SkyBox;
		}
		
		override public function get createEnable()
		{
			return true;
		}
		
		override public function toString()
		{
			return "SkyBox";
		}
		
		//
		override public function newType(scene,dict)
		{
			if(dict == null)
				return new SkyBox(null);
			
			if(scene)
			{
				var dm1 = fileSystem.getResource(dict['positiveX']);
				var dm2 = fileSystem.getResource(dict['negativeX']);
				var dm3 = fileSystem.getResource(dict['positiveY']);
				var dm4 = fileSystem.getResource(dict['negativeY']);
				var dm5 = fileSystem.getResource(dict['positiveZ']);
				var dm6 = fileSystem.getResource(dict['negativeZ']);
				
				if(dm1 && dm2 && dm3 && dm4 && dm5 && dm6)
				{
					var sky = new SkyBox(new CubeMap(	dm1,dm2
														,dm3,dm4
														,dm5,dm6));
					
					sky.material.extra = new Object();
					sky.material.extra.positiveX = dict['positiveX'];
					sky.material.extra.negativeX = dict['negativeX'];
					sky.material.extra.positiveY = dict['positiveY'];
					sky.material.extra.negativeY = dict['negativeY'];
					sky.material.extra.positiveZ = dict['positiveZ'];
					sky.material.extra.negativeZ = dict['negativeZ'];
					
					return sky;
				}
				else
				{
					ILogger.log('please to select six textures!');
					return null;
				}
			}

			
			/*ILogger.log('please to create a view first or click MenuBar->File->New!');
			return null;*/
		}
		
		override public function propertiesToDict(obj,dict:OrderDict,read_only = false,saving = false)
		{
			super.propertiesToDict(obj,dict,read_only,saving);
			
			
			if(read_only == false)
			{
				if(saving == false)
				{
					_title('Sky Textures', dict);
				
					dict.push('positiveX','');
					dict.push('negativeX','');
					dict.push('positiveY','');
					dict.push('negativeY','');
					dict.push('positiveZ','');
					dict.push('negativeZ','');
					var textures = fileSystem.getFileNames('.jpg','.png');
					dict.push(Properties.readMark + 'textuers', textures);
				}
				else
				{
					dict.push('positiveX',obj.material.extra.positiveX);
					dict.push('negativeX',obj.material.extra.negativeX);
					dict.push('positiveY',obj.material.extra.positiveY);
					dict.push('negativeY',obj.material.extra.negativeY);
					dict.push('positiveZ',obj.material.extra.positiveZ);
					dict.push('negativeZ',obj.material.extra.negativeZ);
				}
			}
		}
		
	}
}


import NWish.Interface.ILogger;

import NWish.json.OrderDict;
import NWish.properties.object3ds.MaterialProperties;

import away3d.containers.Scene3D;


class SkyBoxMaterialProperties extends MaterialProperties
{
	public function SkyBoxMaterialProperties(parent)
	{
		super(parent);
	}
	
	override public function propertiesToDict(obj,dict:OrderDict,read_only,saving = false)
	{
		if(read_only == false)
		{
			if(saving)
			{
				saveMaterial(dict,obj.material);
			}
			else
			{
				if(obj.material)
				{
					var mat = 
						{
							'type':mt2str(obj.material),
							'name':obj.material.name
						};
					dict.push('material', mat);
				}
				else
				{
					throw new Error();
					dict.push('material', null);
				}
			}
		}
	}
	
	override public function dictToProperties(dict,obj,scene:Scene3D,isloading)
	{
		var mat = dict['material'];
		
		if(mat == null)
		{//load from file
			loadMaterial(dict,obj,scene);
		}
		else if(mat)
		{
			//newMat(mat,obj);
			ILogger.log('cannot new the other material!');
		}
	}
	
}



